package cn.edu.lingnan.translation.service.impl;

import cn.edu.lingnan.translation.entity.Admin;
import cn.edu.lingnan.translation.entity.vo.AdminLoginVo;
import cn.edu.lingnan.translation.mapper.AdminMapper;
import cn.edu.lingnan.translation.service.AdminService;
import cn.edu.lingnan.translation.utils.MD5;
import cn.edu.lingnan.translation.exception.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Bao
 * @since 2020-10-10
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    /**
     * @Description 管理员登录
     * @Param [adminVo]
     * @Return java.lang.String
     * @Author BAO
     * @Date 2020/10/11 13:07
     */
    @Override
    public String login(AdminLoginVo adminLoginVo) {
        // 获取登录账号和密码
        String username = adminLoginVo.getUsername();
        String password = adminLoginVo.getPassword();
        // 判断账号和密码是否为空
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new MyException(20001, "登录失败");
        }
        // 验证账号和密码
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Admin admin = baseMapper.selectOne(wrapper);
        // 判断查询出来的对象是否为空
        if(admin == null) {
            throw new MyException(20001, "账号未注册");
        }
        // 验证密码
        // 密码用MD5加密，与数据库作比较
        if(!MD5.encrypt(password).equals(admin.getPassword())) {
            throw new MyException(20001, "密码错误");
        }
        // 判断用户是否被禁用
        if(admin.getIsDeleted()) {
            throw new MyException(20001, "登录失败");
        }
        return admin.getAid();
    }
}
